<?php

namespace app\admin\service;

use app\admin\model\ImageCategory;
use app\admin\model\ImageList;
use app\common\consts\RedisConstant;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class ImageService
{
    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'image_title' => 'require',
                'image_src' => 'require',
                'is_forever' => 'require',
            ];
            $message = [
                'category_id.require' => "图片分类不能为空",
                'image_title.require' => "图片名称不能为空",
                'image_src.require' => "图片不能为空",
                'is_forever.require' => "参数错误",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            if (!$data['show_time']) $data['is_forever'] = 1;
            unset($data['imageFile']);
            try {
                if ($data['image_id']) {
                    ImageList::where('image_id', $data['image_id'])->update($data);
                } else {
                    ImageList::create($data);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goStatus()
    {
        $data = Request::post();
        $model = ImageList::where(['image_id' => $data['id']])->find();
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'image_status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goRemove($id)
    {
        $model = ImageList::where(['image_id' => $id])->find();
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEditCate()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $rule = [
                'category_title' => 'require',
            ];
            $message = [
                'category_title.require' => "分类名称不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            unset($data['__token__']);
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            unset($data['imageFile']);
            try {
                $exist = ImageCategory::where(['category_title' => $data['category_title']])
                    ->where('category_id', '<>', $data['category_id'] ?? 0)
                    ->find();
                if ($exist) return ['msg' => '产品名称已存在', 'code' => 201];
                if ($data['category_id']) {
                    ImageCategory::where('category_id', $data['category_id'])->update($data);
                } else {
                    ImageCategory::create($data);
                }
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemoveCate($id)
    {
        $model = ImageCategory::where(['category_id' => $id])->find();
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->delete();
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}